package cn.myra.testbefore;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TestDate {

	public static void main(String[] args) throws ParseException {

		// 定义日期类
		Date date = new Date();
		Date date3 = new Date();
		
		
		//通过Calendar类获取内容
		Calendar cal = Calendar.getInstance();
		//两种设置时间的方式
		cal.setTime(date);
		cal.set(2019, 2, 12, 23, 30, 12);
		//获取时间
		System.out.println(cal.getTime());
		//after方法
		System.out.println(cal.after(date));//false
		
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DATE);
		System.out.println(year + "年" + month + "月" + day + "日");
		int hour = cal.get(Calendar.HOUR);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		int milliSecond = cal.get(Calendar.MILLISECOND);
		System.out.println(hour + "时" + minute + "分" + second + "秒" + milliSecond + "毫秒");

		// 日期与字符串转换1
		String string = "2016-9-23";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date2 = sdf.parse(string);
		System.out.println(date2);

		// 2
		String string2 = sdf.format(date);
		System.out.println(string2);

		// 输出时分秒等信息
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		System.out.println(ts);

		// 数据库中的Date
		// java.sql.Date dateSQL = new java.sql.Date(1998, 8, 30) ;//输出错误 3898-9-30
		java.sql.Date dateSQL = new java.sql.Date(System.currentTimeMillis());
		System.out.println(dateSQL);

		// 格式化输出printf
		System.out.printf("%tc", dateSQL);
		// 星期一 七月 16 14:38:27 CST 2018
		System.out.println();
		System.out.printf("%tF", dateSQL);
		// 2018-07-16
		System.out.println();
		System.out.printf("%tT", dateSQL);
		// 14:38:27
		System.out.println();
		System.out.printf("%tD", dateSQL);
		// 07/16/18
		System.out.println();
		System.out.printf("%tR", dateSQL);
		// 14:38
		System.out.println();
		System.out.printf("%tr", dateSQL);
		// 02:38:27 下午
		System.out.println();

		
		
		char c = '\101';
		System.out.println(c);
	}
}
